/**
 * 
 */
package wdmsim;

import java.util.Vector;

import controlPlane.OBGPSpeaker;
import controlPlane.PCE;


/**
 * @author alissonslp
 *
 */
public class Lightpath extends Link {

	Vector<WDMLink> links;
	private int wavelength;
	PhysicalTopology pt = PhysicalTopology.getInstance();
	
	public Lightpath() {
		this.links = new Vector<WDMLink>();
	}
	
	public Lightpath(WDMLink link) {
		this.links = new Vector<WDMLink>();
		addLink(link);
	}
	
	
	public int getWavelength() {
		return wavelength;
	}

	public static void splitLightpaths(Lightpath l1, Lightpath l2) {
		for(int i=0; i<l2.getNumLinks(); i++) {
			l1.addLink(l2.getLink(i));
		}
	}
	
	public WDMLink getLink(int i) {
		return links.get(i);
	}
	
	public void setWavelength(int wavelength) {
		this.wavelength = wavelength;
	}

	public void addLink(WDMLink link) {
		links.add(link);
	}
	
	public int getNumLinks() {
		return links.size();
	}
	
	public Vector<WDMLink> getLinks() {
		return links;
	}
	
	public boolean releaseLightpath(Flow flow, OBGPSpeaker obgpSpeaker) throws Exception {
		boolean bool = true;
		int size = links.size();
		if(size<=0)
			System.out.print("");
		/*for(int i=0; i<size; i++) {
			if(pt.getDomainFromNode(links.get(i).getSrcId()).getID()==pce.getId()) {
				if(!links.get(i).release(this.wavelength, flow, pce)) {
					bool = false;
					break;
					
				} 
			}			
		}
		if(bool) {
			int i = 0;
			while(i<links.size()) {
				if(pt.getDomainFromNode(links.get(i).getSrcId()).getID()==pce.getId() && 
						links.get(i).getOwn(this.wavelength)==-1) {
					removeLink(links.get(i));
					
				} else {
					i++;
				}
			}
		}*/
		
		return bool;
	}
	
	public boolean releaseLightpath(Flow flow, PCE pce) throws Exception {
		boolean bool = true;
		int size = links.size();
		if(size<=0)
			System.out.print("");
		for(int i=0; i<size; i++) {
			if(pt.getDomainFromNode(links.get(i).getSrcId()).getID()==pce.getId()) {
				if(!links.get(i).release(this.wavelength, flow, pce)) {
					bool = false;
					break;
					
				} 
			}			
		}
		if(bool) {
			int i = 0;
			while(i<links.size()) {
				if(pt.getDomainFromNode(links.get(i).getSrcId()).getID()==pce.getId() && 
						links.get(i).getOwn(this.wavelength)==-1) {
					removeLink(links.get(i));
					
				} else {
					i++;
				}
			}
		}
		return bool;
	}
	
	public boolean releaseLightpath(Flow flow) {
		boolean bool = true;
		for(int i=0; i<links.size(); i++) {
			if(!links.get(i).release(this.wavelength, flow)) {
				bool = false;
				break;	
			}
		}
		if(bool) {
			int i = 0;
			while(i<links.size()) {
				if(links.get(i).getOwn(this.wavelength)==-1) {
					removeLink(links.get(i));
					
				} else {
					i++;
				}
			}
		}	
		return bool;
	}

	private void removeLink(WDMLink wdmLink) {
		links.remove(wdmLink);		
	}

	public int size() {
		return links.size();
	}
	
	public boolean isEmpty() {
		if(size()<=0) {
			return true;
			
		} else {
			return false;
		}
	}
	
	@Override
	public int getWeight() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setWeight(int weight) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getTotalAvailableBandwidth() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see wdmsim.Link#getNumFreeWavelengths()
	 */
	@Override
	public int getNumFreeWavelengths() {
		// TODO Auto-generated method stub
		return 0;
	}
}
